package com.huang.app.antstore.common.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.flexbox.FlexboxLayout;
import com.huang.app.antstore.common.R;

import java.util.HashSet;
import java.util.Set;


/**
 * Created by Huang on 2016/5/23.
 */
public class TagFlowLayout extends FlexboxLayout {
    BaseTagAdapter adapter;
    Set<Integer> selectedItem = new HashSet<>();//多项选择标记
    OnTagClickListener onTagClickListener;
    boolean isMultiSelect;//是否多选,构造方法中已默认多选
    int selectPosition = -1;//单个选择标记
    int tagDefaultBackground;

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        if (selectPosition > -1) {//selectPosition可以看做为上次选中的标记.第一次默认的是没有选中的,所以要判断一次
            adapter.onUnSelect(TagFlowLayout.this, getChildAt(selectPosition), selectPosition);
        }
        adapter.onSelect(TagFlowLayout.this, getChildAt(selectPosition), selectPosition);
        this.selectPosition = selectPosition;

    }

    /**
     * 获取选中的项目索引
     *
     * @return
     */
    public Set<Integer> getSelectedItem() {
        return selectedItem;
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        this.onTagClickListener = onTagClickListener;
    }

    public TagFlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         *  context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout, 0, 0);
         *  参数4:如果设置了参数4.会默认使用参数4style文件里面配置的参数的值
         *  参数3:设置默认样式
         *  参数3优先于参数4.使用参数4,则参数3的值必须为默认的0
         */


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout);
        isMultiSelect = typedArray.getBoolean(R.styleable.TagFlowLayout_multiSelect, true);//获取xml文件中的值,默认多选
        tagDefaultBackground = typedArray.getResourceId(R.styleable.TagFlowLayout_tagDefaultBackground, R.drawable.bg_flexbox_tag_normal);//默认未选中背景
        setFlexDirection(FlexboxLayout.FLEX_DIRECTION_ROW);
        setFlexWrap(FLEX_WRAP_WRAP);
    }

    public void setAdapter(BaseTagAdapter adapter) {
        this.adapter = adapter;
        removeAllViews();
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = adapter.getView(this, i);
            view.setBackgroundResource(tagDefaultBackground);
            addView(view);
            viewClickableSet(view, i);
        }
    }

    private void viewClickableSet(View view, final int position) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTagClickListener != null) {
                    onTagClickListener.tagClickListener(TagFlowLayout.this, v, position);
                }
                if (isMultiSelect) {//多选
                    if (selectedItem.contains(position)) {//已选中的状态点击 取消选中
                        selectedItem.remove(position);
                        adapter.onUnSelect(TagFlowLayout.this, v, position);
                    } else {//为选中的状态点击 选中
                        selectedItem.add(position);
                        adapter.onSelect(TagFlowLayout.this, v, position);
                    }
                } else {//单个选择
                    if (selectPosition > -1) {//selectPosition可以看做为上次选中的标记.第一次默认的是没有选中的,所以要判断一次
                        adapter.onUnSelect(TagFlowLayout.this, getChildAt(selectPosition), selectPosition);
                    }
                    adapter.onSelect(TagFlowLayout.this, v, position);
                    selectPosition = position;
                }

            }
        });
    }

    /**
     * item回调接口
     */
    public interface OnTagClickListener {
        /**
         * item回调
         */
        void tagClickListener(ViewGroup viewGroup, View view, int position);
    }

    /**
     * tag标签适配器
     * Created by Huang on 2016/5/23.
     */
    public abstract class BaseTagAdapter {
        /**
         * 获取每个item的view控件
         *
         * @param viewGroup
         * @param position
         * @return
         */
        public abstract View getView(ViewGroup viewGroup, int position);


        /**
         * 选择过的item
         *
         * @param viewGroup
         * @param view
         * @param position
         */
        public void onSelect(ViewGroup viewGroup, View view, int position) {
        }


        /**
         * 未选择的item
         *
         * @param viewGroup
         * @param view
         * @param position
         */
        public void onUnSelect(ViewGroup viewGroup, View view, int position) {
        }


        /**
         * 获取指定位置的数据
         *
         * @param position
         * @return
         */
        public abstract Object getItem(int position);

        public abstract int getCount();
    }

}
